# from audio.c1 import TestOne
from audio.t1 import AudioAnalyse
from audio.t2 import AudioSpecTwo
from images.x1 import ImageDemo
from optparse import OptionParser
from numpy import *
from numpy import random
from scipy.ndimage import filters
from images.rof import ImageBaseFunc
from scipy.misc import imsave
from images.base import AVBase
from images.sift import IMSift
import sys
import cv2
from PIL import Image
from pylab import *


def test_rof_real(file):
    im = array(Image.open(file).convert('L'))
    U, T = ImageBaseFunc.jkdenoise(im, im)

    figure()
    gray()
    imshow(U)
    axis('equal')
    axis('off')
    show()


def test_x1():
    imgd = ImageDemo()
    # imgd.show()
    # imgd.skimage_show()
    # imgd.custom_show()
    imgd.edge_show()


def test_rof(file):
    im = zeros((500, 500))
    im[100:400,100:400] = 128
    im[200:300, 200:300] = 255
    im = im + 30 * random.standard_normal((500, 500))
    U, T = ImageBaseFunc.jkdenoise(im, im)
    G = filters.gaussian_filter(im, 10)
    imsave("out1.jpg", U)
    imsave("out2.jpg", G)

def test_base_array_image():
    b = AVBase()
    print("Generate Image start")
    b.numpy_array_image()
    print("Generate Image done")

def test_change_image_value(file):
    b = AVBase()
    print("Change Image start")
    b.change_image_value(file)
    print("Change Image end")

def test_harris(file):
    im = array(Image.open(file).convert('L'))
    harrisim = ImageBaseFunc.jk_compute_harris_response(im)
    filtered_coords = ImageBaseFunc.jk_get_harris_points(harrisim, 6)
    ImageBaseFunc.jk_plot_harris_points(im, filtered_coords)

if __name__ == "__main__":

    parser = OptionParser()
    parser.add_option("-t", dest="type")
    parser.add_option("-f", dest="file")
    parser.add_option("-e", dest="fileex")

    (options, args) = parser.parse_args()
    print("type is ", options.type, " file is ", options.file, " file ex is ", options.fileex)
    if options.type == "rof":
        test_rof(options.file)
        exit(0)
    elif options.type == "rofreal":
        test_rof_real(options.file)
    elif options.type == "harris":
        test_harris(options.file)
    elif options.type == "test1":
        test_base_array_image()

    # print("Open audio file %s" % sys.argv[1])
    # aa = AudioSpecTwo(sys.argv[1])
    # aa.test_new()
    elif options.type == "x1":
        test_x1()
    elif options.type == "test2":
        test_change_image_value(options.file)
    elif options.type == "hpf":
        b = AVBase()
        b.hpf_image(options.file)
    elif options.type == "edge":
        b = AVBase()
        src = cv2.imread(options.file)
        dst = src.copy()
        b.stoke_edges(src, dst)
        cv2.imshow('x', src)
        cv2.imshow('y', dst)
        cv2.waitKey(0)
    elif options.type == "create":
        b = AVBase()
        b.create_image(options.file)
    elif options.type == "filter":
        b = AVBase()
        b.conv_filter(options.file)
    elif options.type == "cont":
        b = AVBase()
        b.contour(options.file)
    elif options.type == "contnew":
        b = AVBase()
        b.contour_new(options.file)
    elif options.type == "linedect":
        b = AVBase()
        b.line_detect(options.file)
    elif options.type == "circledetect":
        b = AVBase()
        b.circle_detect(options.file)
    elif options.type == "sift":
        b = IMSift()
        # b.process_image_demo(options.file, "output.sift")
        b.cv_sift(options.file)
    elif options.type == "affine":
        b = ImageBaseFunc()
        b.demo_affine_test(options.file)
    elif options.type == "imagein":
        b = ImageBaseFunc()
        b.demo_image_to_image(options.file, options.fileex)
    elif options.type == "cvaffine":
        b = AVBase()
        b.demo_for_affine(options.file)

    # aa = AudioAnalyse(sys.argv[1])
    # aa.do_analyse()
    # to = TestOne()
    # to.test_one()
